# -*- shell-script -*-

# 40ibm,loc-code - Routines for device-tree with ibm,loc-code property.

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 2004, 2005

# Maintained by Martin Schwenke <martins@au.ibm.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
# $Id: 40ibm,loc-code,v 1.1 2006/04/11 18:38:28 emunson Exp $

[ -n "$source_device_tree" -a \
    \( -f "${source_device_tree}/ibm,loc-code" -o \
       -f "${source_device_tree}/ibm,converged-loc-codes" \) ] || \
    return 0

######################################################################

unset -f get_yl_bus
unset -f get_yl_adapter
unset -f get_yl_adapter_logical

get_yl_DEFAULT ()
{
    # Sets: yl
    
    local meta_type="$1"
    local bus_type="$2"
    local bus_addr="$3"
    local source_node="$4"
    local node="$5"

    dt_manufacture_yl_hook "$source_node"
}

get_yl_device ()
{
    # Sets: yl
    yl=""

    local bus_type="$1"
    local bus_addr="$2"
    local source_node="$3"
    local node="$4"

    dt_manufacture_yl_hook "$source_node"

    # Check if location code wasn't for the device-node, then add to it.
    if [ -n "$yl" -a ! -f "${source_node}/ibm,loc-code" ] ; then
	local id subid
	dt_parse_address "$bus_type" "$bus_addr"
	local yl_suffix
	dt_device_get_yl_suffix "$id" "$subid"
	[ -n "$yl_suffix" ] && \
	    yl="${yl}${yl_suffix}"
    fi

    [ -n "$yl" ]
}

######################################################################

dt_manufacture_yl_hook ()
{
    # Sets: yl
    yl=""

    local source_node="$1"

    local f="${source_node}/ibm,loc-code"
    while [ ! -r "$f" -a "${source_node}" != "$source_device_tree" ] ; do
	source_node="${source_node%/*}" # dirname
	f="${source_node}/ibm,loc-code"
    done

    [ -r "$f" ] && { read yl <"$f" ; true ; }
}

dt_device_get_yl_suffix ()
{
    # Sets: yl_suffix

    local id="$1"
    local subid="$2"

    if [ -n "$id" ] ; then
	local t=$(dec2hex $id)
	yl_suffix="-A${t}"
	if [ -n "$subid" ] && [ "$subid" -ne 0 ] ; then
	    t=$(dec2hex $subid)
	    yl_suffix="${yl_suffix},${t}"
	fi
    fi
}
